package model.dao.registerOrder;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;


import model.service.Enterprise;
import model.service.Person;


public class CustomerDAOImpl {

	String sql = "" ;
	db.dbconnection dbconnection ;
	Connection myConnection ;
	Statement st ;
	
	public Person getPersonByCustomerId(int customerId) {
		Person person = null;
		try {
			
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql ="SELECT subject_fk FROM customer WHERE customer = "+customerId;
			ResultSet resultSet = st.executeQuery(sql);
			int personId = 0;
			while(resultSet.next()){
				personId = resultSet.getInt("subject_fk");
			}
			PersonDAOImpl personDAOImpl = new PersonDAOImpl();
			person = personDAOImpl.getPersonById(personId);
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("CustomerDAOImpl.getPersonByCustomerId()", e.getMessage());
		}
		return person;
	}

	public Enterprise getEnterpriseByCustomerId(int customerId) {
		Enterprise enterprise = null;
		try {
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql ="SELECT subject_fk FROM customer WHERE customer = "+customerId;
			ResultSet resultSet = st.executeQuery(sql);
			int enterpriseId = 0;
			while(resultSet.next()){
				enterpriseId = resultSet.getInt("subject_fk");
			}
			EnterpriseDAOImpl enterpriseDAOImpl = new EnterpriseDAOImpl();
			enterprise = enterpriseDAOImpl.getEnterpriseById(enterpriseId);
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("CustomerDAOImpl.getEnterpriseByCustomerId()", e.getMessage());
		}
		return enterprise;
	}

	public String getCustomerType(int subjectFk) {
		String type = null;
		try {
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql ="SELECT subject_type.type_name FROM customer LEFT JOIN subject_type ON (subject_type.subject_type = customer.subject_type_fk) where customer = "+subjectFk;
			ResultSet resultSet = st.executeQuery(sql);
			while(resultSet.next()){
				type = resultSet.getString("type_name");
			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("CustomerDAOImpl.getCustomerType()", e.getMessage());
		}
		return type;
	}

	public int getCustomerId(int subject, int type) {
		int id = 0;
		try {
			myConnection = dbconnection.getConnection();
			Statement st = myConnection.createStatement();
			String sql = "Select customer from customer where subject_fk ="+subject+" AND subject_type_fk="+type;
			ResultSet resultSet = st.executeQuery(sql);
			
			while(resultSet.next()){
				id = resultSet.getInt("customer");
			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("CustomerDAOImpl.getCustomerId()", e.getMessage());
		}
		return id;
	}

}
